/**
 * Created by panjun7@yunzongnet.com on 2017/05/08.
 */
CreateActivityOtherSettingController = function () {
    this.otherSettingInfo = {};
    this.configInfo = {
        "activityTarget":{
            46:'从未到店消费的顾客',//新会员招募
            22:'所有顾客',//消费送券
            25:'所有顾客',//扫码送券
            35:'所有顾客',
            47:'所有顾客',//单品营销
            43:'{crowdName}',//自定义人群活动
            48:'活动创建后的新开卡用户',//开卡关怀
            51:"所有顾客"//消费送圈人活动
        },
        "obtainType":{
            46:'顾客在投放渠道领券',//新会员招募
            22:'消费后自动发放至会员卡券包',//消费送券
            25:'顾客扫码后领取',//扫码送券
            35:'消费后自动发放至会员卡券包',
            47:'顾客在投放渠道领券',//单品营销
            43:'顾客在投放渠道领券',//自定义人群活动
            48:'顾客在开卡成功页面领取',//开卡关怀
            51:'消费后自动发放至会员卡券包'//消费送圈人活动
        },
        "activityNamePrefix":{
            46:'新会员招募活动',//新会员招募
            22:'消费送券活动',//消费送券
            25:'扫码送券活动',//扫码送券
            35:'消费送券活动',
            47:'单品营销活动',//单品营销
            43:'{crowdName}',//自定义人群活动
            48:'开卡关怀活动',//开卡关怀
            51:'消费送券活动'
        }
    };

    this.initPage = function(isBack) {
        if(!isBack){
            var paramObj = CommonUtil.takeOutParamObject();
            $.extend(this.otherSettingInfo,paramObj);
            
            this.assambleInitValue(paramObj);
        }else{
            var paramObj = CommonUtil.takeOutParamObject();
            $.extend(this.otherSettingInfo,paramObj);
        }

        log4JS.info(JSON.stringify(this.otherSettingInfo));
        CommonUtil.render($("#createOtherSettingPage .page-body"), "otherSettingTPL",{"otherSetting":this.otherSettingInfo});
    };
    //生成默认值
    this.assambleInitValue = function(paramObj){
        var otherSettingInfo = this.otherSettingInfo;
        //活动对象
        otherSettingInfo.activityTarget = this.configInfo.activityTarget[paramObj.activityType];
        otherSettingInfo.activityTarget=otherSettingInfo.activityTarget?otherSettingInfo.activityTarget:"";
        var placeMattern = /^{(\S+)}$/g;
        var placeHolder = otherSettingInfo.activityTarget.match(placeMattern);
        var replaceKey = null;
        if(placeHolder!=null){
            replaceKey = placeMattern.exec(placeHolder[0])[1];//反向找出{}里面的key值
            otherSettingInfo.activityTarget = otherSettingInfo.activityTarget.replace(placeHolder[0],paramObj[replaceKey]);
        }

        //活动名称
        if(!otherSettingInfo.activityName){
            otherSettingInfo.activityName = this.configInfo.activityNamePrefix[otherSettingInfo.activityType]+DateUtil.format(new Date(),"yyyy-MM-dd");
        }

        //发放方式
        otherSettingInfo.obtainType = this.configInfo.obtainType[otherSettingInfo.activityType];

        //券名称
        //全场代金券默认为"券面额"+"品牌名称"+"券类型"；
        //单品代金券默认为"券面额"+"商品名称+"券类型"；
        if(!otherSettingInfo.voucherName){
            if(otherSettingInfo.voucherType == 'MONEY'){
                if(otherSettingInfo.moneyVoucherType == "1"){//全场
                    otherSettingInfo.voucherName = otherSettingInfo.voucherWorthValue+"元"+CommonUtil.nullValueParser(otherSettingInfo,'voucherSubTitle','')+"全场代金券"
                }else if(otherSettingInfo.moneyVoucherType == "2"){//单品
                    otherSettingInfo.voucherName = otherSettingInfo.vorcherWorthValue+"元"+otherSettingInfo.itemName+"元单品代金券"
                }
            }
        }

        // 生效方式：
        // • 消费送券默认为次日生效
        // • 单品营销/扫码送券/开卡关怀/新会员招募默认为当日生效；
        // • 券有效期为指定时间 || 活动类型为新会员招募 || 开卡关怀时生效方式默认均为当日生效，且不可修改（禁用）；
        if(otherSettingInfo.voucherValidateType == 'FIXED' 
            || "46" ==  otherSettingInfo.activityType
            || "48" ==  otherSettingInfo.activityType){
            otherSettingInfo.voucherEffectType = 'IMMEDIATELY';
            otherSettingInfo.voucherEffectTypeDisabled = true;
        }else{
            otherSettingInfo.voucherEffectTypeDisabled = false;
        }

        // 是否可转增：
        // • 消费送券/单品营销/扫码送券默认为是；
        // • 开卡关怀/新会员招募默认为否；不可编辑
        if("46" ==  otherSettingInfo.activityType
            || "48" ==  otherSettingInfo.activityType){
            otherSettingInfo.voucherDonateFlagDisabled = true;
        }else{
            otherSettingInfo.voucherDonateFlagDisabled = false;
        }


        //默认有一条使用说明
        if(!otherSettingInfo.voucherUseInstructions ||  otherSettingInfo.voucherUseInstructions.length==0){
            otherSettingInfo.voucherUseInstructions = ['本代金券不可兑换现金，不可找零。'];
        }

        //券logo预览
        if(otherSettingInfo.voucherLogoUri){
            otherSettingInfo.voucherLogoPreviewUri = App.previewUrlPrefix+otherSettingInfo.voucherLogoUri;
        }

        if(otherSettingInfo.voucherImgUri){
            otherSettingInfo.voucherImgPreviewUri = App.previewUrlPrefix+otherSettingInfo.voucherImgUri;
        }

        //如果是消费送或者消费送圈人活动就不展示券名称和券图片字段
        if("22" == otherSettingInfo.activityType || "51" == otherSettingInfo.activityType || "35" == otherSettingInfo.activityType){
            otherSettingInfo.extInfo = {'showVoucherName':false,'showVoucherImgUrl':false};
        }else{
            otherSettingInfo.extInfo = {'showVoucherName':true,'showVoucherImgUrl':true};
        }

        //新会员招募、开卡关怀的单日、累计领取上限都为每人一次而且不可修改
        if("46" ==  otherSettingInfo.activityType || "48" ==  otherSettingInfo.activityType){
            otherSettingInfo.userWinCountSelect = 2;
            otherSettingInfo.userWinCount = 1;
            otherSettingInfo.userWinCountSelectDisabled = true;


            otherSettingInfo.userWinFrequencySelect = 2;
            otherSettingInfo.userWinFrequencyValue = 1;
            otherSettingInfo.userWinFrequencySelectDisabled = true;
        }else{
            otherSettingInfo.userWinCountSelectDisabled = false;
            otherSettingInfo.userWinFrequencySelectDisabled = false;
        }

    };
    this.saveOtherSettingInfo = function(){
        this.valildateOtherSettingInfo();
    };
    this.valildateOtherSettingInfo = function(){
        var validationResult = {"success":true,"errorMsgs":[]};
        var activityName = $.trim($("#createOtherSettingPage .activityName").val());
        this.otherSettingInfo.activityName = activityName;
        if(CommonUtil.isEmpty(this.otherSettingInfo.activityName)){
            validationResult.success = false;
            validationResult.errorMsgs.push("请填写活动名称");
        }else if(!/^[\u4e00-\u9fa5a-zA-Z0-9\-]+$/.test(activityName)){
            validationResult.success = false;
            validationResult.errorMsgs.push("活动名称不允许包含特殊符号");
        }

        if(this.otherSettingInfo.suitShopsSelect == 2 
            && this.otherSettingInfo.suitShops.split(",").length == 0){
            validationResult.success = false;
            validationResult.errorMsgs.push("请选择适用门店");
        }

        if(this.otherSettingInfo.budgetValueSelect == 2){
            var budgetValue = $.trim($("#createOtherSettingPage .budgetValue").val());
            this.otherSettingInfo.budgetValue = budgetValue;
            if(this.otherSettingInfo.budgetValue == ''){
                validationResult.success = false;
                validationResult.errorMsgs.push("请填写发放数量");
            }else if(!/^[0-9]+$/.test(this.otherSettingInfo.budgetValue)){
                validationResult.success = false;
                validationResult.errorMsgs.push("发放数量不允许输入特殊字符");
            }

            this.otherSettingInfo.budgetValue = parseInt(this.otherSettingInfo.budgetValue);
        }

        if(this.otherSettingInfo.userWinCountSelect == 2){
            var userWinCount = $.trim($("#createOtherSettingPage .userWinCount").val());
            this.otherSettingInfo.userWinCount = userWinCount;
            if(this.otherSettingInfo.userWinCount == ''){
                validationResult.success = false;
                validationResult.errorMsgs.push("请填写累计领取上限");
            }else if(!/^[0-9]+$/.test(this.otherSettingInfo.userWinCount)){
                validationResult.success = false;
                validationResult.errorMsgs.push("累计领取上限不允许输入特殊字符");
            }

            this.otherSettingInfo.userWinCount = parseInt(this.otherSettingInfo.userWinCount);

            if(this.otherSettingInfo.userWinCount>this.otherSettingInfo.budgetValue){
                validationResult.success = false;
                validationResult.errorMsgs.push("累计领取上限不得大于发放数量");
            }
        }

        if(this.otherSettingInfo.userWinFrequencySelect == 2){
            var userWinFrequencyValue = $.trim($("#createOtherSettingPage .userWinFrequencyValue").val());
            this.otherSettingInfo.userWinFrequencyValue = userWinFrequencyValue;
            if(this.otherSettingInfo.userWinFrequencyValue == ''){
                validationResult.success = false;
                validationResult.errorMsgs.push("请填写单日领取上限");
            }else if(!/^[0-9]+$/.test(this.otherSettingInfo.userWinFrequencyValue)){
                validationResult.success = false;
                validationResult.errorMsgs.push("单日领取上限不允许输入特殊字符");
            }


            this.otherSettingInfo.userWinFrequencyValue = parseInt(this.otherSettingInfo.userWinFrequencyValue);

            if(this.otherSettingInfo.userWinFrequencyValue>this.otherSettingInfo.budgetValue){
                validationResult.success = false;
                validationResult.errorMsgs.push("单日领取上限不得大于发放数量");
            }else if(this.otherSettingInfo.userWinFrequencyValue>this.otherSettingInfo.userWinCount){
                validationResult.success = false;
                validationResult.errorMsgs.push("单日领取上限不得大于累计领取上限");
            }
        }

        var $voucherName = $("#createOtherSettingPage .voucherName");
        if($voucherName.length != 0){
            var voucherName = $voucherName .val();
            this.otherSettingInfo.voucherName = voucherName;
            if(CommonUtil.isEmpty(this.otherSettingInfo.voucherName)){
                validationResult.success = false;
                validationResult.errorMsgs.push("请填写券名称");
            }else if(!/^[\u4e00-\u9fa5a-zA-Z0-9\-\.\~]+$/.test(this.otherSettingInfo.voucherName)){
                validationResult.success = false;
                validationResult.errorMsgs.push("券名称不允许输入特殊字符");
            }
        }
        var voucherSubTitle = $("#createOtherSettingPage .voucherSubTitle").val();
        this.otherSettingInfo.voucherSubTitle = voucherSubTitle;
        if(CommonUtil.isEmpty(this.otherSettingInfo.voucherSubTitle)){
            validationResult.success = false;
            validationResult.errorMsgs.push("请填写品牌名称");
        }else if(!/^[\u4e00-\u9fa5a-zA-Z0-9\-]+$/.test(this.otherSettingInfo.voucherSubTitle)){
            validationResult.success = false;
            validationResult.errorMsgs.push("品牌名称不允许输入特殊字符");
        }

        if(CommonUtil.isEmpty(this.otherSettingInfo.voucherLogo)){
            validationResult.success = false;
            validationResult.errorMsgs.push("请选择品牌logo");
        }

        var voucherDonateFlag = $("#createOtherSettingPage .voucherDonateFlag").attr("checked")==true?1:0;
        this.otherSettingInfo.voucherDonateFlag = voucherDonateFlag;

        
        return validationResult;     
    };
    this.selectVoucherLogoCallback = function(selectResult){
        var self = this;
        if(selectResult.code == 0){

            common.loading();
            //上传
            AppUtil.call(AppUtil.APIS.UPLOAD_PICTURE,
                            {data : {},
                             file:(typeof selectResult.resultObject) =="string"?selectResult.resultObject:selectResult.resultObject[0],
                             header:'',
                             url:App.serverUrl +　"app/img/uploadActivityImg.do",
                            },
                            function(result){
                                common.closeLoading();
                                log4JS.info(JSON.stringify(result));
                                if(result.success){
                                    var uploadResult = result.resultObject;
                                    if(uploadResult.success){
                                        var uploadResultObject = uploadResult.resultObject
                                        self.otherSettingInfo.voucherLogo = uploadResultObject.imageId;
                                        self.otherSettingInfo.voucherLogoUri = uploadResultObject.imageUrl;
                                        self.otherSettingInfo.voucherLogoPreviewUri = App.previewUrlPrefix+uploadResultObject.imageUrl;
                       
                                        $("#createOtherSettingPage .voucherLogoPreviewUri").attr("src",self.otherSettingInfo.voucherLogoPreviewUri);
                                    }else{
                                         DialogUtil.dialogWarn(uploadResult.msg);
                                    }   
                                }else{
                                    DialogUtil.dialogWarn(result.msg);
                                }
                            });
        }else if(selectResult.code == -1){
            DialogUtil.dialogWarn(selectResult.msg);
        }
    };
    this.selectVoucherImgCallback=function(selectResult){
        log4JS.info("selectResult:"+JSON.stringify(selectResult));
        var self = this;
        if(selectResult.code == 0){
            common.loading();
            //上传
            AppUtil.call(AppUtil.APIS.UPLOAD_PICTURE,
                            {data : {},
                             file:(typeof selectResult.resultObject) =="string"?selectResult.resultObject:selectResult.resultObject[0],
                             header:'',
                             url:App.serverUrl +　"app/img/uploadActivityImg.do",
                            },
                            function(result){
                                common.closeLoading();
                                log4JS.info(JSON.stringify(result));
                                if(result.success){
                                    var uploadResult = result.resultObject;
                                    if(uploadResult.success){
                                        var uploadResultObject = uploadResult.resultObject
                                        self.otherSettingInfo.voucherImg = uploadResultObject.imageId;
                                        self.otherSettingInfo.voucherImgUri = uploadResultObject.imageUrl;
                                        self.otherSettingInfo.voucherImgPreviewUri = App.previewUrlPrefix+uploadResultObject.imageUrl;
                                         
                                        $("#createOtherSettingPage .voucherImgPreviewUri").show(); 
                                        $("#createOtherSettingPage .voucherImgPreviewUri").prev("span").hide();    
                                        $("#createOtherSettingPage .voucherImgPreviewUri").attr("src",self.otherSettingInfo.voucherImgPreviewUri);
                                    }else{
                                         DialogUtil.dialogWarn(uploadResult.msg);
                                    }         

                                }else{
                                    DialogUtil.dialogWarn(result.msg);
                                }
                            });
        }else if(selectResult.code == -1){
            DialogUtil.dialogWarn(selectResult.msg);
        }
    };
    this.clearInto = function(){
        this.otherSettingInfo = {};
    }

    
};